package sort.cmp;

import jdk.nashorn.internal.ir.WhileNode;

/**
 * @Author kaho
 * @create 2020/12/8
 */
public class 快速排序 {


    public static void quickSort2(int[] arr) {
        sort2(arr, 0, arr.length);
    }

    private static void sort2(int[] arr, int begin, int end) {
        if (end-begin<2) {
            return;
        }
        int pivote = pivotIndex2(arr, begin, end);
        sort2(arr, begin, pivote);
        sort2(arr, pivote+1, end);
    }

    private static int pivotIndex2(int[] arr, int begin, int end) {
        int pivot = arr[begin];
        end--;
        while(begin<end){
            while(begin<end) {
                if (pivot<arr[end]){
                    end--;
                }else {
                    arr[begin++] = arr[end];
                    break;
                }
            }

            while(begin<end) {
                if (pivot<arr[begin]){
                    arr[end--] = arr[begin];
                    break;
                }else {
                    begin++;
                }
            }
        }
        arr[begin] = pivot;
        return begin;
    }


    public static void quickSort(int[] arr) {
        sort1(arr, 0, arr.length);
    }

    private static void sort1(int[] arr, int begin, int end) {
        if (end - begin < 2) {
            return;
        }
        int pivot = pivotIndex1(arr, begin, end);
        sort1(arr, begin, pivot);
        sort1(arr, pivot + 1, end);
    }

    private static int pivotIndex1(int[] arr, int begin, int end) {
        int pivot = arr[begin];
        end--;
        while (begin < end) {
            while (begin < end) {
                if (pivot < arr[end]) {
                    end--;
                } else {
                    arr[begin++] = arr[end];
                    break;
                }
            }

            while (begin < end) {
                if (pivot > arr[begin]) {
                    begin++;
                } else {
                    arr[end--] = arr[begin];
                    break;
                }
            }
        }
        arr[begin] = pivot;
        return pivot;
    }

    public static void quickSort1(int[] arr) {
        sort(arr, 0, arr.length);
    }

    private static void sort(int[] arr, int begin, int end) {
        if (end - begin < 2) {
            return;
        }
        int mid = pivotIndex(arr, begin, end);
        sort(arr, begin, mid);
        sort(arr, mid + 1, end);
    }

    private static int pivotIndex(int[] arr, int begin, int end) {
        int pivot = arr[begin];
        end--;

        while (begin < end) {
            while (begin < end) {
                if (pivot < arr[end]) {
                    end--;
                } else {
                    arr[begin++] = arr[end];
                    break;
                }
            }

            while (begin < end) {
                if (pivot > arr[begin]) {
                    begin++;
                } else {
                    arr[end--] = arr[begin];
                    break;
                }
            }
        }
        arr[begin] = pivot;
        return begin;
    }

    public static void main(String[] args) {
        int[] i = new int[]{25, 9, 4, 1};
        quickSort2(i);
    }
}
